package org.extframework.ld.http;

import java.io.IOException;
import java.io.PrintWriter;

import javax.script.ScriptException;
import javax.servlet.http.HttpServletResponse;

import org.extframework.ld.encryption.JsScriptEngine;
import org.extframework.ld.http.abstr.AbstractEncryptionWebResponse;

/**
 * @author : Dong.Li
 * @created : 2013-1-17
 * @version : 1.0
 * @description :
 */
public class EncryptionWebResponse extends AbstractEncryptionWebResponse {

	public EncryptionWebResponse(HttpServletResponse response) {
		super(response);
	}
	
	

	public void printSuccess(String respText) throws IOException {
		PrintWriter printWriter = getWriter();
		try {
			String text = String.format("{success:true,responseText:\"%s\"}",
					JsScriptEngine.engineCode(respText));
			printWriter.write(text);
		} catch (Exception e) {
			printError("加密错误!");
		}
		endPrint(printWriter);
	}

	public void printError(String respText) throws IOException {
		PrintWriter printWriter = getWriter();
		String text = null;
		try {
//			text = String.format("{success:false,responseText:\"%s\"}",
//					JsScriptEngine.engineCode(respText));
			
			text = String.format("{success:false,responseText:\"%s\"}",respText);
		} catch (Exception e) {
			e.printStackTrace();
		}
		printWriter.write(text);
		endPrint(printWriter);
	}

	public void printJson(String respText) throws IOException {
		setContentType("text/json; charset=UTF-8");
		PrintWriter printWriter = getWriter();
		printWriter.write(respText);
		endPrint(printWriter);
	}

	public void printStr(String text) throws IOException {
		setContentType("text/html; charset=UTF-8");
		PrintWriter printWriter = getWriter();
		printWriter.write(text);
		endPrint(printWriter);
	}

	public void printSuccessJson(String json) throws IOException {
		PrintWriter printWriter = getWriter();
		try {
			String text = String.format("{success:true,%s}",
					JsScriptEngine.engineCode(json));
			printWriter.write(text);
		} catch (Exception e) {
			printError("加密错误!");
		}
		endPrint(printWriter);
	}

	public void printEncryptionJson(String paramString) throws IOException {
		try {
			printJson(JsScriptEngine.engineCode(paramString));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}



	public void printSuccessJ(String paramString) throws IOException {
		PrintWriter printWriter = getWriter();
		try {
			String text = String.format("{success:true,responseText:%s}",
					paramString);
			printWriter.write(text);
		} catch (Exception e) {
			printError("加密错误!");
		}
		endPrint(printWriter);		
	}
	public void printSuccessStr(String paramString) throws IOException {
		PrintWriter printWriter = getWriter();
		try {
			String text = String.format("{success:true,responseText:\"%s\"}",
					paramString);
			printWriter.write(text);
		} catch (Exception e) {
			printError("加密错误!");
		}
		endPrint(printWriter);		
	}

}
